import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Notice } from './notice.entity';
import { Repository } from 'typeorm';
import { UserSys } from 'src/user/userSys.entity';

@Injectable()
export class NoticeService {
    constructor(
        @InjectRepository(Notice)
        public readonly notice: Repository<Notice>
    ) { }
    public async getmine(user: UserSys) {
        return this.notice.find({
            where: {
                user: { id: user.id }
            },
            relations: ["target"],
            order: {
                id: "desc"
            }
        })
    }

    public async create(
        user: UserSys,
        target: UserSys = null,
        title: string,
        context: string
    ) {
        return await this.notice.save(this.notice.create({
            user,
            target,
            title,
            context
        }))
    }
}
